package app.ui;

import app.game.ChessBoard;
import app.game.enums.Chess;
import app.ui.adapter.PutChessAdapter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * @Description 主窗口
 * @Date 2022/4/10 16:48
 * @Created by LYP
 */
public class MainFrame extends JFrame implements Config{

    private ChessBoard chessBoard;

    public boolean isGameStart = false;

    public boolean isP2C = false;

    public Chess comChess = Chess.WHITH_CHESS;

    public java.util.List<JRadioButton> selectors = new ArrayList<>();

    public MainFrame() {
        init();
    }

    private void init() {
        // 初始化窗口
        setSize(2 * BOARD_MARGIN + CHESS_SIZE * BOARD_LINE + 200, 2 * BOARD_MARGIN + CHESS_SIZE * BOARD_LINE + MENU_SIZE);
        setTitle("五子棋");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        FlowLayout flowLayout = new FlowLayout();
        setLayout(null);
        setVisible(true);

        // 初始化棋盘
        Graphics framGraphics = getGraphics();
        chessBoard = new ChessBoard(framGraphics);

        // 增加鼠标事件监听
        PutChessAdapter putChessAdapter = new PutChessAdapter(this);
        addMouseListener(putChessAdapter);

        // 初始化菜单
//        JMenuBar menuBar = new JMenuBar();
//        menuBar.add(new GameMenu(this));
//        menuBar.setSize(2 * BOARD_MARGIN + CHESS_SIZE * BOARD_LINE, MENU_SIZE);
//        menuBar.setLayout(null);
//        menuBar.setVisible(true);
//        add(menuBar);

        // 初始化开始按钮
        JButton startButton = new JButton("开始游戏");
        startButton.setSize(100,40);
        startButton.setLocation(ChessBoard.BOARD_RIGHT + 40,50);
        startButton.setFont(new Font("宋体", Font.PLAIN, 14));
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isGameStart) {
                    JOptionPane.showMessageDialog(null, "对局已开始，请不要重复点击！");
                } else {
                    isGameStart = true;
                    chessBoard.init();
                    chessBoard.drawChessBoard();
                    for (JRadioButton s: selectors){
                        s.setEnabled(false);
                    }
                    if (chessBoard.getNowChess() == comChess) {
                        if (chessBoard.comPutChess(comChess)){
                            handleEnd();
                        }
                    }
                }
            }
        });
        startButton.setVisible(true);
        add(startButton);

        // 初始化模式选择
        ButtonGroup modleGroup = new ButtonGroup();
        JRadioButton p2pModleButton = new JRadioButton("人人模式", true);
        p2pModleButton.setLocation(ChessBoard.BOARD_RIGHT + 40,100);
        p2pModleButton.setSize(100,20);
        p2pModleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isP2C = false;
                //System.out.println(isP2C);
            }
        });
        JRadioButton p2cModleButton = new JRadioButton("人机模式", false);
        p2cModleButton.setLocation(ChessBoard.BOARD_RIGHT + 40,120);
        p2cModleButton.setSize(100,20);
        p2cModleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isP2C = true;
                //System.out.println(isP2C);
            }
        });
        modleGroup.add(p2pModleButton);
        modleGroup.add(p2cModleButton);
        add(p2pModleButton);
        add(p2cModleButton);


        // 初始化人机选项
        ButtonGroup personChessGroup = new ButtonGroup();
        JRadioButton personChessBlackButton = new JRadioButton("我要黑棋", true);
        personChessBlackButton.setLocation(ChessBoard.BOARD_RIGHT + 40,160);
        personChessBlackButton.setSize(100,20);
        personChessBlackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                comChess = Chess.WHITH_CHESS;
                //System.out.println(personChess.getName());
            }
        });
        JRadioButton personChessWhiteButton = new JRadioButton("我要白棋", false);
        personChessWhiteButton.setLocation(ChessBoard.BOARD_RIGHT + 40,180);
        personChessWhiteButton.setSize(100,20);
        personChessWhiteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                comChess = Chess.BLACK_CHESS;
                //System.out.println(personChess.getName());
            }
        });
        personChessGroup.add(personChessBlackButton);
        personChessGroup.add(personChessWhiteButton);
        add(personChessBlackButton);
        add(personChessWhiteButton);

        // 持有选项方便操作
        selectors.add(p2pModleButton);
        selectors.add(p2cModleButton);
        selectors.add(personChessBlackButton);
        selectors.add(personChessWhiteButton);

        // 初始化当前行棋方展示
        JLabel nowChessLable = new JLabel();
        nowChessLable.setText("当前行棋方：黑棋");
        nowChessLable.setSize(150,40);
        nowChessLable.setLocation(ChessBoard.BOARD_RIGHT + 40,200);
        nowChessLable.setFont(new Font("宋体", Font.BOLD, 14));
        nowChessLable.setVisible(true);
        add(nowChessLable);
        chessBoard.setNowChesslabel(nowChessLable);
        paint(framGraphics);
    }

    public void handleEnd(){
        JOptionPane.showMessageDialog(this, "游戏结束，" + chessBoard.getNowChess().getName() + "获胜！");
        this.isGameStart = false;
        for (JRadioButton s: this.selectors){
            s.setEnabled(true);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (chessBoard != null && chessBoard.getNowChesslabel() != null) {
            chessBoard.drawChessBoard();
            System.out.println("绘制了");
        } else {
            System.out.println("为空不绘制");
        }
    }

    public ChessBoard getChessBoard() {
        return chessBoard;
    }
}
